package com.mef.smsplat.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.BaseJdbcDao;
import com.hc24.util.IDUtil;
import com.mef.smsplat.dao.base.BaseSpringJdbcDao;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.GroupInfoDomain;
import com.mef.smsplat.util.Constants;

@Repository
public class GroupInfoDao extends BaseSpringJdbcDao<GroupInfoDomain, String> {
	
	/**
	 * 根据组名 分页查询信息
	 * @param groupName
	 * @param dgm
	 * @return
	 */
	public Page query(String groupName,DataGridModel dgm){
		List<Object> params=new ArrayList<Object>();
		StringBuilder sql=new StringBuilder();
		sql.append("select * from group_info where 1=1");
		if(groupName!=null&&!"".equals(groupName)){
			sql.append(" and group_name like ?");
			params.add("%"+groupName+"%");
		}
		Page<GroupInfoDomain> page= super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params);
		return page;
	}
	
	/**
	 * 根据查询是否唯一
	 * @param groupName
	 * @param id
	 * @return
	 */
	public boolean queryByNiAndId(String groupName,String id){
		String sql="select * from group_info t where t.group_name=? and t.group_id!=?";
		List<GroupInfoDomain> list=super.query(sql, new Object[]{groupName,id});
		if(list.size()>0)
			return false;
		return true;
	}
	
	/**
	 * 批量处理personIds
	 * @param personIds
	 * @param id
	 * @return
	 */
	public void addPersonMember(String[] personIds,String id){
		String sql="insert into group_menber_info(group_menber_id,group_id,menber_id,menber_type) values(?,?,?,?)";
		for (int i = 0; i < personIds.length; i++) {
			super.getJdbcTemplate().update(sql, new Object[]{IDUtil.next()+"",id,personIds[i],Constants.PERSON});
		}
	}
	
	/**
	 * 批量处理companyIds
	 * @param companyIds
	 * @param id
	 * @return
	 */
	public void addCompanyMember(String[] companyIds,String id){
		String sql="insert into group_menber_info(group_menber_id,group_id,menber_id,menber_type) values(?,?,?,?)";
		for (int i = 0; i < companyIds.length; i++) {
			super.getJdbcTemplate().update(sql, new Object[]{IDUtil.next()+"",id,companyIds[i],Constants.COMPANY});
		}
	}
	
	/**
	 * 批量处理
	 * @param ids
	 * @return
	 */
	public void deleteMember(String[] ids){
		String sql="delete from group_menber_info where group_id=?";
		for (int i = 0; i < ids.length; i++) {
			super.getJdbcTemplate().update(sql, new Object[]{ids[i]});
		}
	}
	
	/**
	 * 查询所有组
	 * @return
	 */
	public List<GroupInfoDomain> queryAll(){
		StringBuilder sql=new StringBuilder();
		sql.append("select * from group_info");
		return super.query(sql.toString(),new Object[]{});
	}
	
	@Override
	public BaseJdbcDao getDaoClass() {
		return this;
	}

	@Override
	public Class getEntityClass() {
		return GroupInfoDomain.class;
	}

}
